import { ElMessage } from "element-plus";
import { useFileStore } from "../stores/fileStore";
import type { FileSystemItem } from "../types/fileSystem";

export function useFileOperations(uiState?: {
  hideContextMenu: () => void;
  startRename: (item: FileSystemItem) => void;
  showProperties: () => void;
}) {
  const fileStore = useFileStore();

  // 防止重复消息的标记
  let lastActionTime = 0;
  const actionCooldown = 100; // 100ms内不重复执行

  const isInCooldown = () => {
    const now = Date.now();
    if (now - lastActionTime < actionCooldown) {
      return true;
    }
    lastActionTime = now;
    return false;
  };

  const openItem = (item: FileSystemItem) => {
    if (item.type === "folder") {
      // 修正路径构建逻辑：避免重复斜杠
      let newPath;
      if (fileStore.currentPath === "/") {
        newPath = "/" + item.name;
      } else {
        newPath = fileStore.currentPath + "/" + item.name;
      }

      // 检查路径是否存在
      if (fileStore.fileSystem[newPath]) {
        fileStore.navigate(newPath);
      } else {
        ElMessage.error(`无法访问文件夹: ${item.name}`);
      }
    } else {
      ElMessage.info(`打开文件: ${item.name}`);
    }
  };

  const copyItem = (item?: FileSystemItem) => {
    const targetItem = item || fileStore.selectedItem;

    if (targetItem && !isInCooldown()) {
      fileStore.copyToClipboard(targetItem);
      ElMessage.success(`已复制: ${targetItem.name}`);
      uiState?.hideContextMenu();
    }
  };

  const cutItem = (item?: FileSystemItem) => {
    const targetItem = item || fileStore.selectedItem;
    if (targetItem && !isInCooldown()) {
      fileStore.cutToClipboard(targetItem);
      ElMessage.success(`已剪切: ${targetItem.name}`);
      uiState?.hideContextMenu();
    }
  };

  const pasteItem = () => {
    if (isInCooldown()) {
      return;
    }

    if (fileStore.clipboard.item && fileStore.clipboard.operation) {
      const item = fileStore.clipboard.item;
      const operation = fileStore.clipboard.operation;
      const sourcePath = fileStore.clipboard.sourcePath;
      const currentPath = fileStore.currentPath;

      // 检查是否在同一路径下剪切粘贴
      if (operation === "cut" && sourcePath === currentPath) {
        ElMessage.warning("无法在同一位置剪切粘贴");
        uiState?.hideContextMenu();
        return;
      }

      // 执行粘贴操作
      fileStore.pasteFromClipboard();

      // 显示操作结果
      if (operation === "copy") {
        ElMessage.success(`已复制: ${item.name}`);
      } else if (operation === "cut") {
        ElMessage.success(`已移动: ${item.name}`);
      }
    } else {
      ElMessage.warning("剪贴板为空");
    }
    uiState?.hideContextMenu();
  };

  const deleteItem = (item?: FileSystemItem) => {
    const targetItem = item || fileStore.selectedItem;
    if (targetItem) {
      fileStore.deleteItem(targetItem);
      ElMessage.success(`已删除: ${targetItem.name}`);
      uiState?.hideContextMenu();
    }
  };

  const renameItem = (item?: FileSystemItem) => {
    const targetItem = item || fileStore.selectedItem;
    if (targetItem) {
      uiState?.startRename(targetItem);
      uiState?.hideContextMenu();
    }
  };

  const createNewFolder = () => {
    fileStore.createNewFolder();
    ElMessage.success("已创建新文件夹");
    uiState?.hideContextMenu();
  };

  const createNewFile = () => {
    fileStore.createNewFile();
    ElMessage.success("已创建新文件");
    uiState?.hideContextMenu();
  };

  const showProperties = () => {
    uiState?.showProperties();
    uiState?.hideContextMenu();
  };

  const showOpenWithDialog = () => {
    ElMessage.info("选择打开方式");
    uiState?.hideContextMenu();
  };

  const refresh = () => {
    fileStore.clearSelection();
    ElMessage.success("已刷新");
    uiState?.hideContextMenu(); // 刷新后关闭右键菜单
  };

  return {
    openItem,
    copyItem,
    cutItem,
    pasteItem,
    deleteItem,
    renameItem,
    createNewFolder,
    createNewFile,
    showProperties,
    showOpenWithDialog,
    refresh,
  };
}
